package org.fengling.readingplatform.level.service.impl;

import org.fengling.readingplatform.level.mapper.LevelMapper;
import org.fengling.readingplatform.level.service.LevelService;
import org.fengling.readingplatform.model.vo.level.LevelVo;
import org.fengling.readingplatform.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class LevelServiceImpl implements LevelService {


    @Autowired
    private LevelMapper levelMapper;

    // 获取经验值的方式
    @Override
    public Long getExperience(Long experience) {
        // 从ThreadLocal获取用户信息即可
        Long userId = AuthContextUtil.getUserInfo().getId();
        levelMapper.update(userId, experience);
        return levelMapper.getExperience(userId);
    }

    // 升级的方式
    @Override
    public String getLevel(Long experience) {
        Long userId = AuthContextUtil.getUserInfo().getId();
        String BeforeLevel = levelMapper.getBeforeLevel(userId);
        String NowLevel = levelMapper.getNowLevel(experience);
        if(!BeforeLevel.equals(NowLevel)){
            levelMapper.updateLevel(userId, NowLevel);
        }
        return NowLevel;
    }

    // 获得等级信息的方式
    @Override
    public LevelVo getLevelInfo(String level) {
        return levelMapper.getLevelInfo(level);
    }
}
